package com.platform.enums;

import com.platform.comm.constants.EnumOption;
import com.platform.comm.enums.IEnumOption;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Title: MonitorTaskStatus
 * Description: 交换监控任务状态,兼容批处理作业的状态
 * Date 2020/4/20
 *
 * @author wxw
 * @version v1.0
 */
@EnumOption(name = "batchTaskStatus")
public enum BatchTaskStatus implements IEnumOption<String> {
    NEW(BatchJobStatus.NEW),
    RUNNABLE(BatchJobStatus.RUNNABLE),
    WAITING(BatchJobStatus.WAITING),
    RUNNING(BatchJobStatus.STARTED),
    COMPLETED(BatchJobStatus.COMPLETED),
    FAILED(BatchJobStatus.FAILED);

    private final BatchJobStatus[] jobStatuses;

    BatchTaskStatus(BatchJobStatus... jobStatuses) {
        this.jobStatuses = jobStatuses;
    }

    public static String getName(String status) {
        for (BatchTaskStatus batchTaskStatus : values()) {
            Set<String> set = Arrays.stream(batchTaskStatus.jobStatuses).map(BatchJobStatus::getKey).collect(Collectors.toSet());
            if(set.contains(status)){
                return batchTaskStatus.name();
            }
        }
        return null;
    }

    /**
     * 枚举项 “键”
     *
     * @return key
     */
    @Override
    public String key() {
        return name();
    }

    /**
     * 枚举项 “值”
     *
     * @return value
     */
    @Override
    public String value() {
        switch (this) {
            case COMPLETED: return "成功";
            case FAILED: return "失败";
            case RUNNING: return "运行中";
            case NEW: return "准备";
            case RUNNABLE: return "就绪";
            case WAITING: return "等待";
        }

        return "";
    }

    /**
     * 枚举options
     *
     * @return
     */
    @Override
    public Map<String, String> enums() {
        Map<String, String> result = new LinkedHashMap<>();
        for(BatchTaskStatus batchTaskStatus : values()) {
            result.put(batchTaskStatus.key(), batchTaskStatus.value());
        }
        return result;
    }
}
